package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmsComment;
import com.macro.mall.portal.domain.PmsCommentDetail;
import com.macro.mall.portal.service.PmsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品评价管理Controller
 */
@Controller
@Api(tags = "PmsCommentController")
@Tag(name = "PmsCommentController", description = "商品评价管理")
@RequestMapping("/comment")
public class PmsCommentController {
    @Autowired
    private PmsCommentService commentService;

    @ApiOperation("添加商品评价")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PmsComment comment) {
        int count = commentService.create(comment);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("分页查询商品评价")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsComment>> list(@RequestParam(value = "productId", required = false) Long productId,
                                                    @RequestParam(value = "star", required = false) Integer star,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsComment> commentList = commentService.list(productId, star, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(commentList));
    }
    
    @ApiOperation("查询商品评价，优先显示自己的评价")
    @RequestMapping(value = "/productComments", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> productComments(@RequestParam("productId") Long productId,
                                                    @RequestParam(value = "star", required = false, defaultValue = "0") Integer star,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Map<String, Object> result = commentService.listProductComments(productId, star, pageSize, pageNum);
        return CommonResult.success(result);
    }
    
    @ApiOperation("获取商品评价统计信息（好评、中评、差评数量及好评率）")
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> commentStatistics(@RequestParam("productId") Long productId) {
        Map<String, Object> result = commentService.commentStatistics(productId);
        return CommonResult.success(result);
    }
    
    @ApiOperation("分页查询我的评价")
    @RequestMapping(value = "/myComments", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsCommentDetail>> myComments(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsCommentDetail> commentList = commentService.listMemberComments(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(commentList));
    }
    
    @ApiOperation("获取评价详情（包含商品和订单信息）")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsCommentDetail> getDetail(@PathVariable Long id) {
        PmsCommentDetail commentDetail = commentService.getCommentDetail(id);
        return CommonResult.success(commentDetail);
    }

    @ApiOperation("根据ID查询商品评价")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsComment> getItem(@PathVariable Long id) {
        PmsComment comment = commentService.getItem(id);
        return CommonResult.success(comment);
    }

    @ApiOperation("修改评价显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids,
                                        @RequestParam("showStatus") Integer showStatus) {
        int count = commentService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除评价")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = commentService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
    
    @ApiOperation("添加评价回复")
    @RequestMapping(value = "/reply/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createReply(@RequestParam("commentId") Long commentId,
                                   @RequestParam("content") String content) {
        int count = commentService.createReply(commentId, content);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
} 